package com.lit.common.boot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lit.common.boot.convert.SimpleHttpMessageConverter;
import com.lit.common.util.Springs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import org.springframework.web.servlet.mvc.method.annotation.SimpleRequestResponseBodyMethodProcessor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author panda
 */
public class CommonConfig {

    @Value("${spring.application.name}")
    private String appName;


    @Bean
    @ConditionalOnMissingBean(Springs.class)
    public Springs springs(ApplicationContext applicationContext) {
        Springs springs = new Springs();
        springs.setApplicationContext(applicationContext);
        return springs;
    }

    @Bean
    public SimpleHttpMessageConverter getMappingJackson2HttpMessageConverter() {
        SimpleHttpMessageConverter mappingJackson2HttpMessageConverter = new SimpleHttpMessageConverter();
        //设置日期格式
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(smt);
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //设置中文编码格式
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON_UTF8);
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
        return mappingJackson2HttpMessageConverter;
    }

    // Http Put方式form表单解析过滤器
    @Bean
    public HttpPutFormContentFilter httpPutFormContentFilter() {
        return new HttpPutFormContentFilter();
    }

    @Bean
    public List emptyConfig(RequestMappingHandlerAdapter adapter, List<HttpMessageConverter<?>> converters) {
        adapter.getMessageConverters().clear();
        adapter.getMessageConverters().addAll(converters);
        int i = 0;
        List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
        for (HandlerMethodReturnValueHandler handlerMethodReturnValueHandler : adapter.getReturnValueHandlers()) {
            if (handlerMethodReturnValueHandler instanceof RequestResponseBodyMethodProcessor) {
                SimpleRequestResponseBodyMethodProcessor simpleRequestResponseBodyMethodProcessor = new SimpleRequestResponseBodyMethodProcessor(converters);
                returnValueHandlers.add(simpleRequestResponseBodyMethodProcessor);
            } else {
                returnValueHandlers.add(handlerMethodReturnValueHandler);
            }
        }
        adapter.setReturnValueHandlers(returnValueHandlers);
        return null;
    }
}
